package com.valveinspectionsys.routing.activity;

import java.util.ArrayList;
import java.util.List;

import com.valveinspectionsys.R;
import com.valveinspectionsys.dbhelp.XmlNameHelper;
import com.valveinspectionsys.gridview.ImageSwitcherActivity;
import com.valveinspectionsys.povo.DeviceItem;
import com.valveinspectionsys.povo.DeviceVo;
import com.valveinspectionsys.routing.BaseAdapter.DeviceRoutingCheckAdapter;
import com.valveinspectionsys.routing.db.DeviceRoutingDao;
import com.valveinspectionsys.util.SharedPreferenceUtil;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * RFID异常巡检
 * @author King
 *2013-4-27
 */
public class DeviceRoutingNoRfidActivity extends Activity implements OnClickListener{
    
    public static final int REQUEST_CODE_PAIZHAO=1;

    public ListView  listview;
    
    public DeviceRoutingCheckAdapter adapter;
    
    public Button button;//保存
    
    public Button button1;//拍照
    
    public Button button2;//读卡
    
    public TextView textinfo;//统计信息
    
   public List<Integer> info =new ArrayList<Integer>();
    
    public List<DeviceItem> item =new ArrayList<DeviceItem>();//当前设备册的
    
    public DeviceRoutingDao  dao;
    
    public SharedPreferenceUtil  sp;
    
   
    public DeviceVo  device;//一个设备信息
      
    public TextView textview2;//设备编号
    public TextView textview3;//设备地址
    public TextView textview4;//公称口径
    public TextView textview5;//设备类型

    public EditText  edit;//备注
    
    public int total=0;
    public int nocopytotal=0;
    public String deviceid;
    public boolean isSave=false;
    public String devicelistid;
    

    @Override
    protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.device_routing_check_rfid);
	sp=new SharedPreferenceUtil(this);
	dao=new DeviceRoutingDao(this, sp.getValue(XmlNameHelper.SqliteName));
	deviceid=getIntent().getStringExtra("deviceid");
	devicelistid=getIntent().getStringExtra("devicelistid");
	textinfo=(TextView)findViewById(R.id.device_routing_check_rfid_info);
	textview2=(TextView)findViewById(R.id.device_routing_check_rfid_number);
	textview3=(TextView)findViewById(R.id.device_routing_check_rfid_adds);
	
	textview4=(TextView)findViewById(R.id.device_routing_check_rfid_devicecaliber);
	textview5=(TextView)findViewById(R.id.device_routing_check_rfid_devicetype);
	
	edit=(EditText)findViewById(R.id.device_routing_check_rfid_remark);
	button=(Button)findViewById(R.id.device_routing_check_rfid_save);
	button1=(Button)findViewById(R.id.device_routing_check_rfid_tag);
	button2=(Button)findViewById(R.id.device_routing_check_rfid_paizhao);
	button.setVisibility(View.INVISIBLE);
	button1.setOnClickListener(this);
	button2.setOnClickListener(this);
	button1.setText("保存");
	initDate();
    }
    
    public void initDate(){
	device=dao.queryRfidDevice(null, deviceid, devicelistid);
	total=dao.queryAllDeviceTotal(device.getDeviceListId());
	nocopytotal=dao.queryNoCopyDeviceTotal(device.getDeviceListId());
	textinfo.setText("设备册总数:"+total+"  未巡检数:"+nocopytotal);
	textview2.setText(device.getDeviceNumber());
	textview3.setText(device.getDeviceAdds());
	textview4.setText(device.getDeviceCaliber());
	textview5.setText(device.getDeviceType());
	textview3.setSelected(true);
	edit.setText(device.getRemark());
	listview=(ListView)findViewById(R.id.device_routing_check_rfid_listview);
	item=new ArrayList<DeviceItem>();
	info=new ArrayList<Integer>();
	item=dao.queryItems(device.getDeviceListdeviceitem());
	analyze(device.getCopydata());
	adapter=new DeviceRoutingCheckAdapter(item, info, this);
	listview.setAdapter(adapter);
    }
 
    @Override
    public void onClick(View v) {
	switch (v.getId()) {
	case R.id.device_routing_check_rfid_tag:
	    if(isSelectAll(adapter.getInfo())){
		isSave=true;
		  Intent intent=new Intent(DeviceRoutingNoRfidActivity.this, ImageSwitcherActivity.class);
		     intent.putExtra("otherid", device.getDeviceId());
		     intent.putExtra("status", "1");
		     intent.putExtra("devicelistid", device.getDeviceListId());
		     startActivityForResult(intent, REQUEST_CODE_PAIZHAO);
	    }else{
		 Toast.makeText(DeviceRoutingNoRfidActivity.this, "请检查所有巡检项目！", Toast.LENGTH_SHORT).show();
	     }
	    break;
	case R.id.device_routing_check_rfid_paizhao:
	     Intent intent=new Intent(DeviceRoutingNoRfidActivity.this, ImageSwitcherActivity.class);
	     intent.putExtra("otherid", device.getDeviceId());
	     intent.putExtra("status", "1");
	     intent.putExtra("devicelistid", device.getDeviceListId());
	     startActivityForResult(intent, REQUEST_CODE_PAIZHAO);
	    break;
	default:
	    break;
	}
	
    }
    
    //检测所巡检项目是否选择完毕
    public boolean isSelectAll(List<Integer> list){
	 boolean b=true;
	 
	 for(int i=0;i<list.size();i++){
	   
	     if(list.get(i)==0){
		 return false;
	     }
	 }
	return b;
	 
    }
    public List<Integer> analyze(String infos){
   	System.out.println("-------infos-------------"+infos);
   	info.clear();
   	if(!TextUtils.isEmpty(infos)&&infos.indexOf(",")>0){
   	    String[] str=infos.split(",");
   		
   		for(int i=0;i<str.length;i++){
   		    info.add(Integer.parseInt(str[i]));
   		}
   	}
   	
   	if(info.size()==0){
   	    for(int i=0;i<item.size();i++){
   		info.add(0);
   	    }
   	}
   	return null;
   	
       }
       
    public String checkdata(List<Integer> list){
	 String str="";
	 for(int i=0;i<list.size();i++){
	     str=str+list.get(i)+",";
	 }
	return str.substring(0, str.length()-1);
    }
    
    
    
   
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	    if(requestCode==REQUEST_CODE_PAIZHAO){
		if(resultCode==RESULT_OK){
		    if(isSave){
			       boolean b=dao.insertDeviceData(device.getDeviceId(), checkdata(adapter.getInfo()), "0",edit.getEditableText().toString(),device.getDeviceListId());
				 if(b){
				     Toast.makeText(DeviceRoutingNoRfidActivity.this, "添加成功", Toast.LENGTH_SHORT).show();
				 }else{
				     Toast.makeText(DeviceRoutingNoRfidActivity.this, "添加失败", Toast.LENGTH_SHORT).show();
				 }	
				  isSave=false;
		    }
		  
		}else if(requestCode==RESULT_CANCELED){
		    if(!isSave){
			return;
		    }
		    Toast.makeText(DeviceRoutingNoRfidActivity.this, "你没有拍照不会保存数据", Toast.LENGTH_SHORT).show();
		}
	    }
	super.onActivityResult(requestCode, resultCode, data);
    }
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
	      if(keyCode==KeyEvent.KEYCODE_BACK){
		  setResult(RESULT_OK);
		  finish();
	      }
	return super.onKeyDown(keyCode, event);
    }
    
    @Override
    protected void onDestroy() {
	dao.close();
	super.onDestroy();
    }

   

}
